<?php
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use yii\bootstrap\ActiveForm;
use yii\widgets\LinkPager;
use yii\grid\GridView;
use yii\helpers\Url;
$this->title = '用户管理';
?>
<div class="container">
	<div class="row">
		<?php echo $this->render('_leftnav');?>
		<div class="col-md-10">
			<div class="page-header clearfix">
				<h1 class="pull-left">用户管理</h1>
				<div class="pull-right">
				<a class="btn btn-success btn-sm" data-toggle="modal" data-target="#Admin" id='Admins'>添加新用户</a>
			</div>
		</div>
			<!--<?php echo $this->render('_indexnav');?>-->
			<?php 
				$form = ActiveForm::begin([
					'options' => ['class' => 'form-inline well well-sm',
					'id'=>'comment-search-form'],
					'action'=>Url::to(['user/index']),
					'method'=>'post',
					'fieldConfig' => [
						'template' => '<div class="form-group"><div class="col-md-8 controls"  style="padding-top: 9px">{input}{error}</div></div>'
					], 
				]);
			?>
				<div class="form-group">
					<?php 
						$User=['' => '--关键字类型--','uname' => '用户名','uphone' => '手机号','uemail' => '邮箱号','uip' => '登录ip'];
					?>
					<?= Html::dropDownList('user', $search['user'], ArrayHelper::map($User,'id', 'user'), ['class' => 'form-control user']);?>
				</div>

				<div class="form-group">
					<?= Html::input('text', 'user_name', $search['value'], ['class' => 'form-control','id' => 'user_name','placeholder' => '关键字']) ?>
				</div>

				<?= Html::submitButton('搜索', ['class' => 'btn btn-primary','id'=>'user-search-btn']) ?>
				<a class="btn btn-primary mhs" id="export">导出用户</a>
			<?php ActiveForm::end(); ?> 
			<table id="user-table" class="table table-striped table-hover" data-search-form="#user-search-form">
				<thead>
					<tr>
						<th>用户名</th>
						<th>手机号</th>
						<th>Email</th>
						<th>注册时间</th>
						<th>最近登录</th>
						<th>操作</th>
					</tr>
				</thead>
				<tbody>
					<?php if($data){?>
					<?php foreach($data as $key => $val){?>
						<tr id="user-table-tr-14">
							<td>
								<strong>
									<a href="javascript:;" role="show-user" data-toggle="modal" data-target="#modal" data-url="http://dcoffee.edusoho.com.cn/admin/user/14"><?= Html::encode($val['user_name']) ?></a>
								</strong>
								
								<?php if($val['user_status'] == "0"){?>
									<span id="<?php echo Html::encode($val['user_id']) ?>"><label class="label label-danger">禁</label></span>
								<?php }else{?>
									<span id="<?php echo Html::encode($val['user_id']) ?>"><label class="label label-danger"></label></span>
								<?php }?>
								<br>
								<span class="text-muted text-sm">
								</span>
							</td>
							<td>
								<?php if($val['user_phone']){
									echo Html::encode($val['user_phone']);
								}else{
									echo "--";	
								}?>
								<!--  <?= Html::encode($val['user_phone']) ?>  -->
								<span class="text-success"><!-- (已绑定) --></span>
							</td>
							<td>
								<?php if($val['user_email']){
									echo Html::encode($val['user_email']);
								}else{
									echo "--";	
								}?><br>
							</td>
							<td>
								<span class="text-sm">
								<?php if($val['user_regtime']){
									echo date("Y-m-d H:i:s",Html::encode($val['user_regtime']));
								}else{
									echo "--";	
								}?></span>
								<br>
								<span class="text-muted text-sm">
									<?php if($val['user_regip']){?>
										<a class="text-muted text-sm" href="http://www.baidu.com/s?wd=<?php echo Html::encode($val['user_regip']) ?>" target="_blank"><?php echo Html::encode($val['user_regip']) ?></a>
									<?php }else{
										echo "--";
									}?>
									北京
								</span>
								<span></span>
							</td>
							<td>
								<span class="text-sm">
								<?php if($val['user_lastlogin']){
									echo date("Y-m-d H:i:s",Html::encode($val['user_lastlogin']));
								}else{
									echo "--";	
								}?></span>
								<br>
								<span class="text-muted text-sm">
									<?php if($val['user_lastip']){?>
										<a class="text-muted text-sm" href="http://www.baidu.com/s?wd=<?php echo Html::encode($val['user_lastip']) ?>" target="_blank"><?php echo Html::encode($val['user_lastip']) ?></a>
									<?php }else{
										echo "--";
									}?>
									北京
								</span>
							</td>
							<td>
								<div class="btn-group">
									<a href="javascript:;"  class="btn btn-default btn-sm UserInfo" data-toggle="modal" data-target="#UserInfo" type="<?= Html::encode($val['user_id'])?>">查看</a>
									<?php if($val['user_status'] == "1"){
									?>
										<a href="javascript:;"  class="btn btn-default btn-sm Off" type="<?= Html::encode($val['user_id'])?>">禁用</a>
									<?php }else{ ?>
										<a href="javascript:;"  class="btn btn-default btn-sm On" type="<?= Html::encode($val['user_id'])?>">启用</a>
									<?php }?>
									
								</div>
							</td>
						</tr>
					<?php }}else{?>
						<tr>
							<td colspan="20">
								<div class="empty">暂无用户记录</div>
							</td>
						</tr>
					<?php }?>
				</tbody>
			</table>
			<div align="right">
				<?php echo LinkPager::widget([
					'pagination' => $pagination,
					'prevPageLabel'=>'上一页',
					'nextPageLabel'=>'下一页',
				]);?>
			</div>
		</div>
	</div>
</div>
<div class="modal" id="Admin">
	<div class="modal-dialog ">
		<div class="modal-content">
			<div class="modal-header">
				<button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
				<h4 class="modal-title">添加用户</h4>
			</div>
			<div class="modal-body">
				<?php 
					$form = ActiveForm::begin([
						'options' => ['class' => 'form-horizontal',
						'id'=>'Users-form'],
						'action'=>Url::to(['user/usercreate']),
						'method'=>'post',
						'fieldConfig' => [
							'template' => '<div class="form-group"><center><label class="col-md-2 control-label" for="type-name-field">{label}</label></center><div class="col-md-8 controls">{input}{error}</div></div>'
						], 
					]);
				?>
					<?= $form->field($model, 'user_name',['inputOptions'=>['placeholder'=>'请输入用户名']])->textInput(['maxlength' => true]) ?>

					<?= $form->field($model, 'user_email',['inputOptions'=>['placeholder'=>'请输入邮箱']])->textInput(['maxlength' => true]) ?>

					<?= $form->field($model, 'user_pwd',['inputOptions'=>['placeholder'=>'请输入密码']])->passwordInput(['maxlength' => true]) ?>

					<?= $form->field($model, 'user_repwd',['inputOptions'=>['placeholder'=>'请输入确认密码']])->passwordInput(['maxlength' => true]) ?>
					<div class="modal-footer">
						<?= Html::submitButton('保存', ['class' => 'btn btn-success','id'=>'user-create-btn']) ?>
						<button data-dismiss="modal" class="btn btn-link pull-right" type="button">取消</button>
					</div>
				<?php ActiveForm::end(); ?>
			</div>
		</div>
	</div>
</div>
<div class="modal" id="UserInfo"></div>
<script type="text/javascript">
	//导出
	$("#export").click(function(){
		var user=$(".user").val();
		var user_name=$("#user_name").val();
		location.href="<?php echo \Yii::$app->urlManager->createUrl('user/export')?>&user="+user+"&user_name="+user_name;
	})
	//详情
	$(".UserInfo").click(function(){
		var user_id = $(this).attr('type');
		$.ajax({
			type: "POST",
			url: "<?php echo Url::to(['user/info']);?>",
			data: "user_id="+user_id,
			dataType: 'json',
			success: function(msg){
				if (msg.code == 200)
				{
					$("#UserInfo").html(msg.data);
				}else if(msg.code == 403)
				{
					$("#UserInfo").html('<div class="modal-dialog " style="width:170px;"><div class="modal-content"><div class="modal-header"><button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button><h4 class="modal-title"><font color="red">'+msg.msg+'</font></h4></div></div></div>');
				}
			}
		});
	})
	//点击禁用
	$(document).on("click",".Off",function(){
		var user_id = $(this).attr('type');
		var self = $(this)
		$.ajax({
			type: "POST",
			url: "<?php echo Url::to(['user/status']);?>",
			data: "user_id="+user_id+"&user_status=0",
			dataType: 'json',
			success: function(msg){
				if (msg.code == 200) {
					self.replaceWith('<a href="javascript:;"  class="btn btn-default btn-sm On" type="'+user_id+'">启用</a>')
					$("#"+user_id).html('<label class="label label-danger">禁</label>')
				}else if(msg.code == 400)
				{
					alert("修改失败")	
				}else if(msg.code == 403) 
				{
					alert('暂无权限')
				}
			}
		});
	})
	//点击启用
	$(document).on("click",".On",function(){
		var user_id = $(this).attr('type');
		var self = $(this)
		$.ajax({
			type: "POST",
			url: "<?php echo Url::to(['user/status']);?>",
			data: "user_id="+user_id+"&user_status=1",
			dataType: 'json',
			success: function(msg){
				if (msg.code == 200) {
					self.replaceWith('<a href="javascript:;"  class="btn btn-default btn-sm Off" type="'+user_id+'">禁用</a>')
					$("#"+user_id).html('<label class="label label-danger"></label>')
				}else if(msg.code == 400)
				{
					alert("修改失败")	
				}else if(msg.code == 403) 
				{
					alert('暂无权限')
				}
			}
		});
	})
</script>